package com.lcz.authority.admin.mapper;

import com.lcz.authority.common.core.mybatis.BaseMapperX;
import com.lcz.authority.common.core.mybatis.LambdaQueryWrapperX;
import com.lcz.authority.common.core.mybatis.MPJLambdaWrapperX;
import com.lcz.authority.common.domain.entity.*;
import org.apache.ibatis.annotations.Mapper;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lcz
 * @desc 岗位信息 数据层
 * @mail lcz_0130@163.com
 * @date 2024/06/17
 */
@Mapper
public interface SysPostMapper extends BaseMapperX<SysPostEntity> {
    /**
     * 查询岗位数据集合
     *
     * @param post 岗位信息
     * @return 岗位数据集合
     */
    default List<SysPostEntity> selectPostList(SysPostEntity post) {
        //	    <include refid="selectPostVo"/>
        //		<where>
        //			<if test="postCode != null and postCode != ''">
        //				AND post_code like concat('%', #{postCode}, '%')
        //			</if>
        //			<if test="status != null and status != ''">
        //				AND status = #{status}
        //			</if>
        //			<if test="postName != null and postName != ''">
        //				AND post_name like concat('%', #{postName}, '%')
        //			</if>
        //		</where>
        return selectList(new LambdaQueryWrapperX<SysPostEntity>()
                .likeIfPresent(SysPostEntity::getPostCode, post.getPostCode())
                .eqIfPresent(SysPostEntity::getStatus, post.getStatus())
                .likeIfPresent(SysPostEntity::getPostName, post.getPostName()));

    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    default List<SysPostEntity> selectPostAll() {
        return selectList(new LambdaQueryWrapperX<>());
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    default SysPostEntity selectPostById(Long postId) {
        return selectById(postId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    default List<Long> selectPostListByUserId(String userId) {
        //select p.post_id
        //        from sys_post p
        //	        left join sys_user_post up on up.post_id = p.post_id
        //	        left join sys_user u on u.user_id = up.user_id
        //	    where u.user_id = #{userId}
        MPJLambdaWrapperX<SysPostEntity> sysPostEntityLambdaQueryWrapper = new MPJLambdaWrapperX<>();
        sysPostEntityLambdaQueryWrapper.select(SysPostEntity::getPostId)
                .leftJoin(SysUserPostEntity.class, SysUserPostEntity::getPostId, SysPostEntity::getPostId)
                .leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserPostEntity::getUserId)
                .eq(SysUserEntity::getUserId, userId);
        List<SysPostEntity> sysPostEntities = selectList(sysPostEntityLambdaQueryWrapper);
        if (sysPostEntities != null && !sysPostEntities.isEmpty()) {
            return selectList(sysPostEntityLambdaQueryWrapper).stream().map(SysPostEntity::getPostId).collect(Collectors.toList());
        }
        return null;

    }

    /**
     * 根据用户ID查询岗位
     *
     * @param userId 用户ID
     * @return 岗位列表
     */
    default List<SysPostEntity> selectPostPermissionByUserId(String userId) {
        MPJLambdaWrapperX<SysPostEntity> wrapperX = new MPJLambdaWrapperX<>();
        wrapperX.selectAll(SysPostEntity.class);
        wrapperX.leftJoin(SysUserPostEntity.class, SysUserPostEntity::getPostId, SysRoleEntity::getRoleId);
        wrapperX.leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserRoleEntity::getUserId);
        wrapperX.eq(SysRoleEntity::getDelFlag, "0");
        wrapperX.eq(SysUserEntity::getUserId, userId);
        return selectList(wrapperX);
    }

    /**
     * 查询用户所属岗位组
     *
     * @param userName 用户名
     * @return 结果
     */
    default List<SysPostEntity> selectPostsByUserName(String userName) {
        //select p.post_id, p.post_name, p.post_code
        //		from sys_post p
        //			 left join sys_user_post up on up.post_id = p.post_id
        //			 left join sys_user u on u.user_id = up.user_id
        //		where u.user_name = #{userName}
        return selectList(new MPJLambdaWrapperX<SysPostEntity>()
                .select(SysPostEntity::getPostId, SysPostEntity::getPostName, SysPostEntity::getPostCode)
                .leftJoin(SysUserPostEntity.class, SysUserPostEntity::getPostId, SysPostEntity::getPostId)
                .leftJoin(SysUserEntity.class, SysUserEntity::getUserId, SysUserPostEntity::getUserId)
                .eq(SysUserEntity::getUserName, userName));
    }

    /**
     * 删除岗位信息
     *
     * @param postId 岗位ID
     * @return 结果
     */
    default int deletePostById(Long postId) {
        return deleteById(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    default int deletePostByIds(Long[] postIds) {
        return deleteBatchIds(Arrays.asList(postIds));
    }

    /**
     * 修改岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    default int updatePost(SysPostEntity post) {
        return updateById(post);
    }

    /**
     * 新增岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    default int insertPost(SysPostEntity post) {
        return insert(post);
    }

    /**
     * 校验岗位名称
     *
     * @param postName 岗位名称
     * @return 结果
     */
    default SysPostEntity checkPostNameUnique(String postName) {
        return selectOne(new LambdaQueryWrapperX<SysPostEntity>()
                .eq(SysPostEntity::getPostName, postName));
    }

    /**
     * 校验岗位编码
     *
     * @param postCode 岗位编码
     * @return 结果
     */
    default SysPostEntity checkPostCodeUnique(String postCode) {
        return selectOne(new LambdaQueryWrapperX<SysPostEntity>()
                .eq(SysPostEntity::getPostCode, postCode));
    }
}
